{extend name="common/base"/}
<!-- 主体 -->
{block name="body"}
<div class="p-3">
	<form class="layui-form gg-form-bar border-t border-x">
		<div class="layui-inline">
			<label class="layui-form-label">SPU</label>
			<div class="layui-input-inline">
				<input type="text" name="spu_id" value="{:input('spu_id')}"  class="layui-input" >
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">开始日期</label>
			<div class="layui-input-inline">
				<input type="text" name="start_date" id="start_date"
					   value="{:input('start_date') ?: date('Y-m-d')}"
					   placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">截止日期</label>
			<div class="layui-input-inline">
				<input type="text" name="end_date" id="end_date"
					   value="{:input('end_date') ?: date('Y-m-d')}"
					   placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
			</div>
		</div>

		<div class="layui-input-inline" style="width:150px">
			<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="table-search"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
			<button type="reset" class="layui-btn layui-btn-reset" lay-filter="table-reset">清空</button>
		</div>
	</form>
	<div class="layui-btn-container">
		<span class="layui-btn  layui-btn-normal" id="fileUpload" style="margin-left:10px;">导入商品</span>
		<span class="layui-btn layui-bg-purple biao">导出表一</span>
		<span class="layui-btn  add-menu">导出表二</span>
		<span class="layui-btn layui-bg-orange supplier">导出供应商</span>
		<span class="layui-btn layui-btn-danger" id="batchDeleteBtn"><i class="layui-icon">&#xe640;</i> 批量删除</span>
		<span class="layui-btn layui-btn-warning" id="batchDeleteMarkedBtn"><i class="layui-icon">&#xe640;</i>批量删除标记数据</span>
<!--     <span class="layui-btn layui-btn-primary" id="clearMarkedBtn"><i class="layui-icon">&#xe632;</i>清除所有标记</span>-->
		<span class="layui-btn  add-prompt">绑定店铺名称</span>
<!--		<span class="layui-btn layui-btn-info" id="showMarkedInfo"><i class="layui-icon">&#xe615;</i>显示标记信息</span>-->
		<span class="layui-btn  layui-btn-normal" id="fileUpload_pc" style="margin-left:10px;">导入说明文</span>
	</div>
	<table class="layui-hide" id="goods" lay-filter="goods"></table>
</div>

<script type="text/html" id="status">
	<i class="layui-icon {{#  if(d.status == 1){ }}layui-icon-ok green{{#  } else { }}layui-icon-close red{{#  } }}"></i>
</script>
<script type="text/html" id="is_home">
	<i class="layui-icon {{#  if(d.is_home == 1){ }}layui-icon-ok green{{#  } else { }}layui-icon-close red{{#  } }}"></i>
</script>

<!-- 操作列模板 - 新增"最新"按钮 + 编辑按钮加标识类 -->
<script type="text/html" id="barDemo">
	<div class="layui-btn-group m_barDemo">
		<!-- 编辑按钮新增 edit-operation 类，用于样式控制 -->
		<a class="layui-btn layui-btn-normal edit-operation" lay-event="edit">编辑</a>
		<a onclick="imgPreview('{{ d.spu_id }}','{{ d.create_time }}',this)" class="layui-btn yulan"  lay-event="datalist">预览</a>
		<a class="layui-btn layui-btn-danger mark-delete" lay-event="markDelete" data-id="{{d.spu_id}}">标记</a>
		<a class="layui-btn layui-btn-danger" lay-event="del">删除</a>
		<a  onclick="pcPreview('{{ d.pc_url }}',this)" class="layui-btn pc_btn" >说明文</a>
<!--		<a class="layui-btn layui-btn-success latest-operation" lay-event="latest" data-id="{{d.spu_id}}">最新</a>-->
	</div>
</script>

<div id="prompt_style" style="display:none">
	<div class="layui-form" style="margin: 20px;" lay-filter="shopForm">
		<select id="shop_id" name="shop_id" lay-verify="required" lay-search lay-filter="shopSelect">
			<option value="0">请选择店铺名称</option>
			{volist name="address_name" id="v"}
			<option value="{$v.id}">{$v.name}</option>
			{/volist}
		</select>
	</div>
</div>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<style>
	.m_barDemo a.layui-btn{ padding:0 10px; line-height: 32px; margin-left: 10px !important;}
	.m_barDemo a:first-child{ margin-left:0 !important; }
	.layui-table-blue {
		background-color: #e6f7ff !important; /* 编辑行高亮 */
	}
	.marked-for-delete {
		background-color: #fff2f2 !important; /* 标记删除行背景（浅红色） */
		transition: background-color 0.3s ease;
	}
	.latest-operation-row {
		background-color: #f0fff4 !important; /* 最新操作行背景（浅绿色） */
		transition: background-color 0.3s ease;
	}
	/* 新增：编辑记录行背景（浅蓝色） */
	.edited-operation-row {
		background-color: #e6f7ff !important;
		transition: background-color 0.3s ease;
	}
	.mark-delete {
		background-color: #7991E8;
		color: white;
		transition: all 0.3s ease;
	}
	.mark-delete.marked {
		background-color: #5879B4;
	}
	.latest-operation {
		background-color: #a233c6;
		color: white;
		transition: all 0.3s ease;
	}
	.latest-operation.active {
		background-color: #a233c6;
	}
	/* 新增：编辑按钮激活状态样式 */
	.edit-operation.active {
		background-color: #1E9FFF; /* 编辑按钮激活色（深一点的蓝色） */
	}
	.layui-btn-warning{
		background-color: #7991E8;
	}
	.y_active{ color: #000}
	.pc_btn{ background-color: #1c6ca1;}
	.pc_active{ color: #FFF700;}
	.layui-table td, .layui-table th{ padding: 10px 0 ! important;}
</style>
<script>
	const moduleInit = ['tool','tablePlus'];

	// 存储键名常量 - 新增编辑记录存储键
	const MARKED_STORAGE_KEY = 'markedDeleteIds';
	const LATEST_OPERATION_KEY = 'latestOperationId'; // 最新操作ID
	const EDITED_OPERATION_KEY = 'editedOperationId'; // 新增：编辑操作ID

	// 调试日志函数
	function debugLog(message, data) {
		console.log(`[标记功能调试] ${message}`, data || '');
	}

	// 新增第三个参数 btn，接收当前点击的按钮元素
	function imgPreview(spuId, time, btn) {
		const mday = time.slice(5, 7) + time.slice(8, 10);
		window.open("/admin/files/index?keywords=" + mday + "/" + spuId, '_blank');

		$('.yulan').removeClass('y_active').text('预览'); // 清除所有高亮
		$(btn).addClass('y_active').text('已预览');// 给当前按钮添加高亮
	}
	function pcPreview(url, btn) {
		$('.pc_btn').removeClass('pc_active');
		$(btn).addClass('pc_active');
		window.open(url, '_blank');
	}


	function gouguInit() {
		var table = layui.tablePlus,
				tool = layui.tool,
				upload = layui.upload,
				form = layui.form;

		// 初始化本地存储（删除标记）
		function initMarkedStorage() {
			try {
				const stored = localStorage.getItem(MARKED_STORAGE_KEY);
				if (!stored || stored === 'undefined') {
					localStorage.setItem(MARKED_STORAGE_KEY, JSON.stringify([]));
				} else {
					const data = JSON.parse(stored);
					if (!Array.isArray(data)) throw new Error('存储数据不是数组');
				}
			} catch (e) {
				debugLog('本地存储初始化失败，重置为空数组', e);
				localStorage.setItem(MARKED_STORAGE_KEY, JSON.stringify([]));
			}
		}

		// 初始化最新操作存储
		function initLatestOperationStorage() {
			try {
				const stored = localStorage.getItem(LATEST_OPERATION_KEY);
				if (stored === null || stored === 'undefined') {
					localStorage.setItem(LATEST_OPERATION_KEY, '');
				}
			} catch (e) {
				debugLog('最新操作存储初始化失败，重置为空', e);
				localStorage.setItem(LATEST_OPERATION_KEY, '');
			}
		}

		// 新增：初始化编辑操作存储
		function initEditedOperationStorage() {
			try {
				const stored = localStorage.getItem(EDITED_OPERATION_KEY);
				if (stored === null || stored === 'undefined') {
					localStorage.setItem(EDITED_OPERATION_KEY, '');
				}
			} catch (e) {
				debugLog('编辑操作存储初始化失败，重置为空', e);
				localStorage.setItem(EDITED_OPERATION_KEY, '');
			}
		}

		// 获取标记的ID列表
		function getMarkedIds() {
			try {
				const stored = localStorage.getItem(MARKED_STORAGE_KEY);
				const ids = stored ? JSON.parse(stored) : [];
				return Array.isArray(ids) ? ids : [];
			} catch (e) {
				debugLog('获取标记ID失败', e);
				return [];
			}
		}

		// 保存标记的ID列表
		function saveMarkedIds(ids) {
			try {
				localStorage.setItem(MARKED_STORAGE_KEY, JSON.stringify(ids));
			} catch (e) {
				debugLog('保存标记ID失败', e);
			}
		}

		// 获取最新操作的ID
		function getLatestOperationId() {
			try {
				const id = localStorage.getItem(LATEST_OPERATION_KEY) || '';
				return id;
			} catch (e) {
				debugLog('获取最新操作ID失败', e);
				return '';
			}
		}

		// 保存最新操作的ID
		function saveLatestOperationId(id) {
			try {
				localStorage.setItem(LATEST_OPERATION_KEY, id);
			} catch (e) {
				debugLog('保存最新操作ID失败', e);
			}
		}

		// 新增：获取编辑操作的ID
		function getEditedOperationId() {
			try {
				const id = localStorage.getItem(EDITED_OPERATION_KEY) || '';
				return id;
			} catch (e) {
				debugLog('获取编辑操作ID失败', e);
				return '';
			}
		}

		// 新增：保存编辑操作的ID
		function saveEditedOperationId(id) {
			try {
				localStorage.setItem(EDITED_OPERATION_KEY, id);
			} catch (e) {
				debugLog('保存编辑操作ID失败', e);
			}
		}

		// 修复：清除最新操作标记（新增文本重置）
		function clearLatestOperation() {
			saveLatestOperationId('');
			$('.latest-operation-row').removeClass('latest-operation-row');
			$('.latest-operation.active').removeClass('active');
			$('.latest-operation').text('最新'); // 关键修复：重置按钮文本
		}

		// 新增：清除编辑操作标记
		function clearEditedOperation() {
			saveEditedOperationId('');
			$('.edited-operation-row').removeClass('edited-operation-row');
			$('.edit-operation.active').removeClass('active');
			$('.edit-operation').text('编辑'); // 重置按钮文本
		}

		// 标记行为删除状态
		function markRowAsDeleted(spuId) {
			const tableView = $('#goods').next('.layui-table-view');
			if (!tableView.length) return;

			let trElement = tableView.find(`td[data-field="spu_id"][data-value="${spuId}"]`).closest('tr');
			if (!trElement.length) {
				tableView.find('.layui-table-body tr').each(function() {
					const td = $(this).find('td[data-field="spu_id"]');
					if (td.text().trim() === spuId) {
						trElement = $(this);
						return false;
					}
				});
			}

			if (trElement && trElement.length) {
				trElement.addClass('marked-for-delete');
				trElement.find('.mark-delete').addClass('marked').text('已标记');
			}
		}

		// 标记行为最新操作状态
		function markRowAsLatest(spuId) {
			const tableView = $('#goods').next('.layui-table-view');
			if (!tableView.length) return;

			// 先清除当前页所有最新标记
			tableView.find('.latest-operation-row').removeClass('latest-operation-row');
			tableView.find('.latest-operation.active').removeClass('active');
			$('.latest-operation').text('最新');

			// 为目标行添加最新标记
			let trElement = tableView.find(`td[data-field="spu_id"][data-value="${spuId}"]`).closest('tr');
			if (!trElement.length) {
				tableView.find('.layui-table-body tr').each(function() {
					const td = $(this).find('td[data-field="spu_id"]');
					if (td.text().trim() === spuId) {
						trElement = $(this);
						return false;
					}
				});
			}

			if (trElement && trElement.length) {
				trElement.addClass('latest-operation-row');
				trElement.find('.latest-operation').addClass('active').text('已最新');
			}
		}

		// 新增：标记行为编辑状态
		function markRowAsEdited(spuId) {
			const tableView = $('#goods').next('.layui-table-view');
			if (!tableView.length) return;

			// 先清除当前页所有编辑标记
			tableView.find('.edited-operation-row').removeClass('edited-operation-row');
			tableView.find('.edit-operation.active').removeClass('active');
			$('.edit-operation').text('编辑');

			// 为目标行添加编辑标记
			let trElement = tableView.find(`td[data-field="spu_id"][data-value="${spuId}"]`).closest('tr');
			if (!trElement.length) {
				tableView.find('.layui-table-body tr').each(function() {
					const td = $(this).find('td[data-field="spu_id"]');
					if (td.text().trim() === spuId) {
						trElement = $(this);
						return false;
					}
				});
			}

			if (trElement && trElement.length) {
				trElement.addClass('edited-operation-row');
				trElement.find('.edit-operation').addClass('active').text('已编辑');
			}
		}

		// 初始化所有存储（新增编辑存储初始化）
		initMarkedStorage();
		initLatestOperationStorage();
		initEditedOperationStorage(); // 新增：初始化编辑存储

		// 渲染表格
		layui.pageTable = table.render({
			elem: '#goods',
			title: '商品列表',
			toolbar: '#toolbarDemo',
			url: '/admin/goods/datalist',
			page: true,
			limit: 20,
			cellMinWidth: 80,
			cols: [
				[
					{type: 'checkbox', fixed: 'left'},
					{field: 'id', align: 'center', title: 'ID', width: 80, visible: false},
					{field: 'spu_id', title: 'SPU', align: 'center', width: 80},
					{field: 'title', align: 'center', title: '商品名称'},
					{field: 'shop_name', align: 'center', title: '店铺名称', width: 80},
					{field: 'sku_key', title: '规格1名称', align: 'center', width: 80},
					{field: 'sku_value', title: '规格1值'},
					{field: 'create_time', title: '导入时间', align: 'center', width: 160},
					{
						fixed: 'right',
						field: 'right',
						title: '操作',
						ignoreExport:true,
						width: 400, // 微调宽度容纳按钮文本变化
						toolbar: '#barDemo',
						align: 'center'
					}
				]
			],
			// 表格渲染完成后恢复所有标记状态（新增编辑标记恢复）
			done: function(res, curr, count){
				// 恢复删除标记状态
				const markedIds = getMarkedIds();
				if (markedIds.length > 0) {
					res.data.forEach(item => {
						if (markedIds.includes(item.spu_id)) markRowAsDeleted(item.spu_id);
					});
				}

				// 恢复最新操作标记状态
				const latestId = getLatestOperationId();
				if (latestId) markRowAsLatest(latestId);

				// 新增：恢复编辑标记状态
				const editedId = getEditedOperationId();
				if (editedId) markRowAsEdited(editedId);
			}
		});

		// 监听表格行操作
		table.on('tool(goods)', function(obj) {
			const data = obj.data;
			switch(obj.event){
				case 'edit':
					$('.layui-table-box').find('tr').removeClass('layui-table-blue');
					obj.tr.addClass('layui-table-blue');
					// 新增：触发编辑记录标记
					toggleEditedOperation(data.spu_id, obj.tr);
					tool.side('/admin/goods/edit?id='+data.id);
					break;
				case 'del':
					layer.confirm('确定要删除该记录吗?', {icon: 3, title: '提示'}, function(index) {
						let callback = function (e) {
							layer.msg(e.msg);
							if (e.code == 0) {
								obj.del();
								// 从标记列表移除
								const markedIds = getMarkedIds();
								const newIds = markedIds.filter(id => id !== data.spu_id);
								saveMarkedIds(newIds);

								// 清除最新/编辑标记（新增编辑标记判断）
								if (getLatestOperationId() === data.spu_id) clearLatestOperation();
								if (getEditedOperationId() === data.spu_id) clearEditedOperation();
							}
						}
						tool.delete("/admin/goods/del", { spu_id: data.spu_id }, callback);
						layer.close(index);
					});
					break;
				case 'markDelete':
					toggleMarkDelete(data.spu_id, obj.tr); // 切换删除标记
					break;
				case 'latest':
					toggleLatestOperation(data.spu_id, obj.tr); // 切换最新标记
					break;
			}
			return false;
		});

		// 切换标记状态核心函数
		function toggleMarkDelete(spuId, trElem) {
			const markedIds = getMarkedIds();
			const index = markedIds.indexOf(spuId);

			if (index > -1) {
				// 取消标记
				markedIds.splice(index, 1);
				trElem.removeClass('marked-for-delete');
				trElem.find('.mark-delete').removeClass('marked').text('标记');
				layer.msg('已取消标记');
			} else {
				// 添加标记
				markedIds.push(spuId);
				trElem.addClass('marked-for-delete');
				trElem.find('.mark-delete').addClass('marked').text('已标记');
				layer.msg('已标记删除');
			}
			saveMarkedIds(markedIds);
		}

		// 切换最新操作状态（唯一）
		function toggleLatestOperation(spuId, trElem) {
			const currentLatestId = getLatestOperationId();

			if (currentLatestId === spuId) {
				clearLatestOperation();
				layer.msg('已取消最新操作标记');
			} else {
				clearLatestOperation(); // 先清除所有最新标记
				saveLatestOperationId(spuId);
				trElem.addClass('latest-operation-row');
				trElem.find('.latest-operation').addClass('active').text('已最新');
				layer.msg('已标记为最新操作');
			}
		}

		// 新增：切换编辑操作状态（唯一）
		function toggleEditedOperation(spuId, trElem) {
			const currentEditedId = getEditedOperationId();

			if (currentEditedId === spuId) {
				clearEditedOperation();
				layer.msg('已取消编辑标记');
			} else {
				clearEditedOperation(); // 先清除所有编辑标记
				saveEditedOperationId(spuId);
				trElem.addClass('edited-operation-row');
				trElem.find('.edit-operation').addClass('active').text('已编辑');
				layer.msg('已记录为当前编辑记录');
			}
		}

		// 批量删除选中数据（新增编辑标记联动）
		$('#batchDeleteBtn').on('click', function() {
			const selectedIds = getSelectedSpuIds();
			if (selectedIds.length === 0) {
				layer.msg('请选择要删除的数据', {icon: 2});
				return;
			}
			layer.confirm(`确定要删除选中的 ${selectedIds.length} 条数据吗？`, {
				icon: 3, title: '删除确认'
			}, function(index) {
				let callback = function (e) {
					layer.closeAll();
					layer.msg(e.msg);
					if (e.code == 0) {
						// 从标记列表移除已删除ID
						let markedIds = getMarkedIds();
						markedIds = markedIds.filter(id => !selectedIds.includes(id));
						saveMarkedIds(markedIds);

						// 清除最新/编辑标记（新增编辑标记判断）
						if (selectedIds.includes(getLatestOperationId())) clearLatestOperation();
						if (selectedIds.includes(getEditedOperationId())) clearEditedOperation();

						$('[lay-filter="table-reset"]').click();
					}
				}
				tool.delete("/admin/goods/del", {spu_id:selectedIds.join(',')}, callback);
			});
		});

		// 批量删除标记数据（新增编辑标记联动）
		$('#batchDeleteMarkedBtn').on('click', function() {
			const markedIds = getMarkedIds();
			if (markedIds.length === 0) {
				layer.msg('没有标记要删除的数据', {icon: 2});
				return;
			}
			layer.confirm(`确定要删除标记的 ${markedIds.length} 条数据吗？`, {
				icon: 3, title: '删除确认'
			}, function(index) {
				let callback = function (e) {
					layer.closeAll();
					layer.msg(e.msg);
					if (e.code == 0) {
						// 清除最新/编辑标记（新增编辑标记判断）
						if (markedIds.includes(getLatestOperationId())) clearLatestOperation();
						if (markedIds.includes(getEditedOperationId())) clearEditedOperation();

						saveMarkedIds([]);
						$('[lay-filter="table-reset"]').click();
					}
				}
				tool.delete("/admin/goods/del", {spu_id:markedIds.join(',')}, callback);
			});
		});

		// 清除所有标记（新增编辑标记清除）
		$('#clearMarkedBtn').on('click', function() {
			const markedIds = getMarkedIds();
			const hasAnyMark = markedIds.length > 0 || getLatestOperationId() || getEditedOperationId();

			if (!hasAnyMark) {
				layer.msg('没有标记的数据', {icon: 2});
				return;
			}

			layer.confirm('确定要清除所有标记（删除/最新/编辑）吗？', {
				icon: 3, title: '确认'
			}, function(index) {
				// 清除删除标记
				saveMarkedIds([]);
				$('.marked-for-delete').removeClass('marked-for-delete');
				$('.mark-delete.marked').removeClass('marked').text('标记');

				// 清除最新/编辑标记（新增编辑标记清除）
				clearLatestOperation();
				clearEditedOperation();

				layer.close(index);
				layer.msg('已清除所有标记');
			});
		});

		// 显示标记信息（新增编辑记录显示）
		$('#showMarkedInfo').on('click', function() {
			const markedIds = getMarkedIds();
			const latestId = getLatestOperationId();
			const editedId = getEditedOperationId(); // 新增：获取编辑ID

			let info = `当前标记的删除ID总数: ${markedIds.length}\n`;
			info += `当前编辑记录ID: ${editedId || '无'}\n`; // 新增：显示编辑记录
			info += `最新操作ID: ${latestId || '无'}\n\n`;
			info += `删除标记ID列表:\n${markedIds.join(', ') || '无'}`;

			layer.alert(info, {
				title: '标记信息'
			});
		});

		// 导出表二
		$('.add-menu').on('click', function(){
			const ids = getSelectedIds();
			const start_date = $('#start_date').val();
			const end_date = $('#end_date').val();
			window.location.href = `/admin/api/export?start_date=${start_date}&end_date=${end_date}&ids=${ids}`;
			return false;
		});

		// 导出供应商
		$('.supplier').on('click', function(){
			const ids = getSelectedIds();
			const start_date = $('#start_date').val();
			const end_date = $('#end_date').val();
			window.location.href = `/admin/api/exportSupplier?start_date=${start_date}&end_date=${end_date}&ids=${ids}`;
			return false;
		});

		// 绑定店铺名称
		$('.add-prompt').on('click', function(){
			const ids = getSelectedIds();
			if(!ids){
				layer.msg('请选择要绑定的商品', {icon: 2});
				return false;
			}
			$('#prompt_style').css('display','block');
			const index = layer.open({
				type: 1,
				content: $('#prompt_style').html(),
				area: ['500px', '280px'],
				title: '绑定店铺名称',
				btn: ['确定', '取消'],
				btnAlign: 'r',
				btn1: function(layero){
					const $layero = $(layero);
					const shopId = layui.form.val('shopForm').shop_id;
					if(!shopId){
						layer.msg('请选择店铺', {icon: 2});
						return false;
					}
					var data = {ids: ids,shop_id: shopId};
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							layer.close(index);
							layui.pageTable.reload();
						}
					}
					tool.post("/admin/goods/bind", data, callback);
				},
				success: function(layero, index, that){
					const $layero = $(layero);
					layui.form.render('select', $layero.find('form[lay-filter="shopForm"]')[0]);
					$('#prompt_style').css('display','none');
				}
			});
		});

		// 导出表一
		$('.biao').on('click', function(){
			const ids = getSelectedSpuIds().join(',');
			const start_date = $('#start_date').val();
			const end_date = $('#end_date').val();
			window.location.href = `/admin/api/exportProducts?start_date=${start_date}&end_date=${end_date}&spu_id=${ids}`;
			return false;
		});

		// 监听搜索提交
		form.on('submit(searchform)', function(data) {
			layui.pageTable.reload({
				where: data.field,
				page: {curr: 1}
			});
			return false;
		});

		// 导入商品
		var uploadInst = upload.render({
			elem: '#fileUpload'
			,url: "/admin/api/uploadExcel"
			, accept: 'file'
			, exts: 'jpeg|jpg|png|gif|doc|docx|ppt|pptx|xls|xlsx|pdf||rar|7z|txt|mp4|psd|csv'
			, before: function (obj) {
				layer.msg('上传中...', { time: 3600000 });
			}
			,done: function (res) {
				if(res.code == 1){
					layer.msg(res.msg);
				}else{
					layer.msg(res.msg, '', function(){
						location.reload();
					});
				}
			}, error: function () {
				layer.msg('上传失败');
			}
		});
		//导入PC端说明文
		var uploadInst = upload.render({
			elem: '#fileUpload_pc'
			,url: "/admin/api/uploadExcel_pc"
			, accept: 'file'
			, exts: 'jpeg|jpg|png|gif|doc|docx|ppt|pptx|xls|xlsx|pdf||rar|7z|txt|mp4|psd|csv'
			, before: function (obj) {
				layer.msg('上传中...', { time: 3600000 });
			}
			,done: function (res) {
				if(res.code == 1){
					layer.msg(res.msg);
				}else{
					layer.msg(res.msg, '', function(){
						location.reload();
					});
				}
			}, error: function () {
				layer.msg('上传失败');
			}
		});

		// 日期选择器
		layui.use('laydate', function(){
			var laydate = layui.laydate;
			laydate.render({elem: '#start_date'});
			laydate.render({elem: '#end_date'});
		});

		// 工具函数：获取选中行的ID
		function getSelectedIds() {
			const ids = [];
			$('input[name=layTableCheckbox]:checked').each(function() {
				const id = $(this).closest('tr').find('td:eq(1)').text().trim();
				if(id) ids.push(id);
			});
			return ids.join(',');
		}

		// 工具函数：获取选中行的SPU_ID
		function getSelectedSpuIds() {
			const ids = [];
			$('input[name=layTableCheckbox]:checked').each(function() {
				const id = $(this).closest('tr').find('td:eq(2)').text().trim();
				if(id) ids.push(id);
			});
			return ids;
		}
	}
</script>
{/block}
<!-- /脚本 -->